// 添加地址页面
var basePage = require('../../../common/BasePage');
var area = require('../../../utils/area');
var util = require('../../../utils/util');
Page({
    behaviors: [basePage],
    /**
     * 页面的初始数据
     */
    data: {
        areaList: area.areaList,
        addrs :[],
        region: [],
        /**显示区域选择 */
        showArea: false,
        state_arr: {
            add: {
                submit_tip: '确 认 提 交',
                state: 'add',
                api: '/home/user/new_addr/add' //请求地址
            },
            update: {
                submit_tip: '确 认 更 新',
                state: 'update',
                api: '/home/user/new_addr/update' //请求地址
            }
        },
        current_state: 'add',
        submit_tip: '确 认 提 交',
        openSetting: false,
        addr: '',
        house: '',
        location: "", //定位地址
        contact: '', //联系人
        mobile: '', //电话

        lng: 0,
        lat: 0,
        addr_id: 0,
        /**请求参数 */
        reqData: {
            province_code: '',
            city_code: '',
            district_code: '',
            province: '',
            city: '',
            district: '',
            latitude: 0,
            longitude: 0
        }
    },
    /**选择好区域 */
    area_ok(e) {
        console.log("area_ok", e);
        let areaArr = e.detail.values;
        let reqData = this.data.reqData;
        //初始化省市县数据
        reqData.province = areaArr[0].name;
        reqData.province_code = areaArr[0].code;
        reqData.city = areaArr[1].name;
        reqData.city_code = areaArr[1].code;
        reqData.district = areaArr[2].name;
        reqData.district_code = areaArr[2].code;
        this.setData({
            addr: reqData.province + reqData.city + reqData.district,
            showArea: false,

        })
    },

    submit_click(e) {
        let check_arr = [{
                name: 'addr',
                tip: '请选择地址'
            },
            {
                name: "location",
                tip: "请选择详细地址"
            },
            {
                name: 'house',
                tip: '请输入门牌号'
            },
            {
                name: 'contact',
                tip: '请输入联系人'
            },
            {
                name: 'mobile',
                tip: '请输入联系电话'
            }
        ]; //检查数组
        let data = {
            addr_id: this.data.addr_id,
            // lat: this.data.lat,
            // lng: this.data.lng,
            province_code: this.data.reqData.province_code,
            city_code: this.data.reqData.city_code,
            district_code: this.data.reqData.district_code,
            province: this.data.reqData.province,
            city: this.data.reqData.city,
            district: this.data.reqData.district,
            lng: this.data.longitude,
            lat: this.data.latitude,
            location: this.data.location

        }; //上传给后台的数据
        for (let i = 0; i < check_arr.length; i++) { //为空检测
            let check = check_arr[i];
            // console.log(this.data[check.name]);
            if (!this.data[check.name]) {
                return wx.showToast({
                    title: check.tip,
                    icon: 'none'
                });
            }
            data[check.name] = this.data[check.name];
        }
        data.house= util.replaceStr(data.house)
        data.contact= util.replaceStr(data.contact)
        data.mobile= util.replaceStr(data.mobile)
        this.request({
            loadingTip: '加密上传...',
            url: this.data.host + this.data.state_arr[this.data.current_state].api,
            data: data,
            method: 'POST',
        }, this.submit_ok);
    },
    //提交成功
    submit_ok(res) {
        console.log(res);
        wx.showToast({
            title: '添加成功',
            icon: 'none'
        });
        wx.navigateBack({
            delta: 1,
        });
        if (this.data.current_state == 'update') { //如果是更新状态，分派通知
            const eventChannel = this.getOpenerEventChannel();
            eventChannel.emit('update_addr', {
                addr: this.data.addr,
                house: this.data.house,
                location: this.data.location,
                contact: this.data.contact,
                mobile: this.data.mobile
            });
        }
    },

    // },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data.options = options;
        console.log("addaddr:", options);
        this.getAreas()
        if (options) {
            let that = this;
            if (options.state == 'update') { //更新模式

                wx.getStorage({
                    key: 'addr',
                    success(res) {
                        // console.log("获取缓存的地址",res);
                        // console.log(res.data);
                        let addr = res.data;
                        if (addr && addr.addr_id) {
                            let reqData = that.data.reqData;
                            //初始化省市县数据
                            reqData.province = addr.province
                            reqData.province_code = addr.province_code
                            reqData.city = addr.city
                            reqData.city_code = addr.city_code
                            reqData.district = addr.district
                            reqData.district_code = addr.district_code
                            that.setData({
                                addr: addr.addr,
                                house: addr.house,
                                contact: addr.contact, //联系人
                                mobile: addr.mobile, //电话
                                reqData: reqData,
                                longitude: addr.lng,
                                latitude: addr.lat,
                                location: addr.location,
                                addr_id: addr.addr_id,
                                current_state: 'update',
                                submit_tip: that.data.state_arr.update.submit_tip
                            });
                            // console.log("current_state",that.data)
                        }
                    }
                })

            }

        }
    },
    getAreas() {
        let that = this
        wx.request({
            url: that.data.host+"/uploads/gaode.json",
            header: {},
            success(res) {
                let result = res.data.districts[0].districts
                let arrs = [{
                    values: result
                }, {
                    values: result[0].districts,
                    defaultIndex: 0,
                }, {
                    values: result[0].districts[0].districts,
                    defaultIndex: 0,
                }]
                console.log("arrs",arrs)
                that.setData({
                    addrs: arrs
                })
            }
        })
    },
    // 省市区变动
    onAreaChange(e) {
        const {
            picker,
            value,
            index
        } = e.detail;
        if (index === 0) {
            // 修改省
            picker.setColumnValues(1, value[0].districts);
            picker.setColumnValues(2, value[0].districts[0].districts);
        } else if (index === 1) {
            // 修改市
            picker.setColumnValues(2, value[1].districts);
        }
    },
    // 确认选择省市区
    onAreaConfirm(e) {
        let areaArr = e.detail.value;
        let reqData = this.data.reqData;
        //初始化省市县数据
        reqData.province = areaArr[0].name;
        reqData.province_code = areaArr[0].adcode;
        reqData.city = areaArr[1].name;
        reqData.city_code = areaArr[1].adcode;
        reqData.district = areaArr[2].name;
        reqData.district_code = areaArr[2].adcode;
        this.setData({
            addr: reqData.province + reqData.city + reqData.district,
            showArea: false,

        })



        // let value = e.detail.value
        // let addr_value = `${value[0].name}${value[1].name}${value[2].name}`;
        // this.setData({
        //     province: value[0].name, // 省份
        //     city: value[1].name, // 城市
        //     district: value[2].name, // 区县
        //     province_adcode: value[0].adcode,
        //     city_adcode: value[1].adcode,
        //     district_adcode: value[2].adcode,
        //     area: addr_value,
        //     showArea: false,
        // })
    },
    onArea() {
        var that = this
        wx.chooseLocation({
            success(res) {
                console.log("选择地址", res)
                that.setData({
                    latitude: res.latitude,
                    longitude: res.longitude,
                    location: res.name
                })
            },
            fail(err) {
                console.log("错误", err)
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})